﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SeaGateComp : DockContent
    {
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectBGHDataTabID = 0;
        //private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();
        //protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];

        public Frm_SeaGateComp()
        {
            InitializeComponent();
        }

        private void Frm_SeaGateComp_Load(object sender, EventArgs e)
        {
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }



        private void buttonSeaGateCompAddr_Click(object sender, EventArgs e)
        {
            Frm_SeaGateCompAddr frm_SeaGateCompAddr = new Frm_SeaGateCompAddr();
            frm_SeaGateCompAddr.StartPosition = FormStartPosition.CenterScreen;
            frm_SeaGateCompAddr.ShowDialog();
        }

        private void buttonSeaGateCompContactPerson_Click(object sender, EventArgs e)
        {
            Frm_SeaGateCompConP frm_SeaGateCompConP = new Frm_SeaGateCompConP();
            frm_SeaGateCompConP.StartPosition = FormStartPosition.CenterScreen;
            frm_SeaGateCompConP.ShowDialog();
        }

        private void buttonSeaGateCompConsume_Click(object sender, EventArgs e)
        {
            Frm_SeaGateCompConsume frm_SeaGateCompConsume = new Frm_SeaGateCompConsume();
            frm_SeaGateCompConsume.StartPosition = FormStartPosition.CenterScreen;
            frm_SeaGateCompConsume.ShowDialog();
        }

        private void ShowAllRecordInDataGridView()
        {
             ultraGrid1.DataSource = null;
             m_ds = m_bllBGHDataTab.GetListWithAddr("");
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = m_ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;

            }
        }

        private void buttonShowAllRecords_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewSeaGateCompList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //string strBGHDataTabID = this.dataGridViewSeaGateCompList.Rows[this.dataGridViewSeaGateCompList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            //m_nSelectBGHDataTabID = Convert.ToInt32(strBGHDataTabID, 10);
            //cellclickBGH();
        }

        private void cellclickBGH()
        {
            DGBY.Model.BGHDataTab modelBGHDataTab = m_bllBGHDataTab.GetModel(m_nSelectBGHDataTabID);
            if (modelBGHDataTab != null)
            {
                this.textBoxSeaGateCompName.Text = modelBGHDataTab.BGHName;
                this.textBoxCompAbbr.Text = modelBGHDataTab.BGHNameJX;
                this.textBoxSeaGateCompPhone.Text = modelBGHDataTab.BGHTel;
                this.textBoxSeaGateCompFax.Text = modelBGHDataTab.BGHFax;
                this.textBoxManagerName.Text = modelBGHDataTab.ManagerName;
                this.textBoxManagerPhone.Text = modelBGHDataTab.ManagerTel;
                this.richTextBoxNote.Text = modelBGHDataTab.Note;

                //新增字段
                this.ck_wts.Checked = modelBGHDataTab.wts;
                this.ck_lld.Checked = modelBGHDataTab.lld;
                this.ck_tyd.Checked = modelBGHDataTab.tyd;
                this.ck_rcd.Checked = modelBGHDataTab.rcd;
                this.ck_fxt.Checked = modelBGHDataTab.fxt;
                this.ck_td.Checked = modelBGHDataTab.td;
                this.ck_ckht.Checked = modelBGHDataTab.ckht;
            }
            else
            {
                this.textBoxSeaGateCompName.Text = "";
                this.textBoxCompAbbr.Text = "";
                this.textBoxSeaGateCompPhone.Text = "";
                this.textBoxSeaGateCompFax.Text = "";
                this.textBoxManagerName.Text = "";
                this.textBoxManagerPhone.Text = "";
                this.richTextBoxNote.Text = "";

                //新增字段
                this.ck_wts.Checked = false;
                this.ck_lld.Checked = false;
                this.ck_tyd.Checked = false;
                this.ck_rcd.Checked = false;
                this.ck_fxt.Checked = false;
                this.ck_td.Checked = false;
                this.ck_ckht.Checked = false;
            }
        }

        //private void dataGridViewSeaGateCompList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        //{
        //    {
        //        using (SolidBrush b = new SolidBrush(dataGridViewSeaGateCompList.RowHeadersDefaultCellStyle.ForeColor))
        //        {
        //            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
        //        }
        //    }
        //}

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BGHDataTab modelBGHDataTab = new DGBY.Model.BGHDataTab();
                modelBGHDataTab.BGHName = this.textBoxSeaGateCompName.Text;
                modelBGHDataTab.BGHNameJX = this.textBoxCompAbbr.Text;
                modelBGHDataTab.BGHTel = this.textBoxSeaGateCompPhone.Text;
                modelBGHDataTab.BGHFax = this.textBoxSeaGateCompFax.Text;
                modelBGHDataTab.ManagerName = this.textBoxManagerName.Text;
                modelBGHDataTab.ManagerTel = this.textBoxManagerPhone.Text;
                modelBGHDataTab.Note = this.richTextBoxNote.Text;

                //新增字段
                modelBGHDataTab.wts = this.ck_wts.Checked;
                modelBGHDataTab.lld = this.ck_lld.Checked;
                modelBGHDataTab.tyd = this.ck_tyd.Checked;
                modelBGHDataTab.rcd = this.ck_rcd.Checked;
                modelBGHDataTab.fxt = this.ck_fxt.Checked;
                modelBGHDataTab.td  = this.ck_td.Checked;
                modelBGHDataTab.ckht = this.ck_ckht.Checked;

                int nResult = m_bllBGHDataTab.Add(modelBGHDataTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelBGHDataTab.BGHName);

                    m_nSelectBGHDataTabID = m_bllBGHDataTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBGHDataTab.GetModel(m_nSelectBGHDataTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.BGHDataTab modelBGHDataTab = m_bllBGHDataTab.GetModel(m_nSelectBGHDataTabID);
                //modelBGHDataTab.BGHDataTabID = m_nSelectBGHDataTabID;
                modelBGHDataTab.BGHName = this.textBoxSeaGateCompName.Text;
                modelBGHDataTab.BGHNameJX = this.textBoxCompAbbr.Text;
                modelBGHDataTab.BGHTel = this.textBoxSeaGateCompPhone.Text;
                modelBGHDataTab.BGHFax = this.textBoxSeaGateCompFax.Text;
                modelBGHDataTab.ManagerName = this.textBoxManagerName.Text;
                modelBGHDataTab.ManagerTel = this.textBoxManagerPhone.Text;
                modelBGHDataTab.Note = this.richTextBoxNote.Text;

                //新增字段
                modelBGHDataTab.wts = this.ck_wts.Checked;
                modelBGHDataTab.lld = this.ck_lld.Checked;
                modelBGHDataTab.tyd = this.ck_tyd.Checked;
                modelBGHDataTab.rcd = this.ck_rcd.Checked;
                modelBGHDataTab.fxt = this.ck_fxt.Checked;
                modelBGHDataTab.td = this.ck_td.Checked;
                modelBGHDataTab.ckht = this.ck_ckht.Checked;

                m_bllBGHDataTab.Update(modelBGHDataTab);

                Frm_Main.Log2DB(this.Text, "修改", modelBGHDataTab.BGHName);
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllBGHDataTab.GetModel(m_nSelectBGHDataTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBGHDataTab.Delete(m_nSelectBGHDataTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickBGH();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;


            string strBGHDataTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectBGHDataTabID = Convert.ToInt32(strBGHDataTabID, 10);
            cellclickBGH();
        }

        private void buttonRecvPerson_Click(object sender, EventArgs e)
        {
            Frm_SeaGateCompRecvP frm_SeaGateCompRecvP = new Frm_SeaGateCompRecvP();
            frm_SeaGateCompRecvP.StartPosition = FormStartPosition.CenterScreen;
            frm_SeaGateCompRecvP.ShowDialog();
        }

       
    }
}